//
//  TZVideoPlayerController.m
//  TZImagePickerController
//
//  Created by 谭真 on 16/1/5.
//  Copyright © 2016年 谭真. All rights reserved.
//

#import "TZVideoPlayerController.h"
#import <ZFPlayer.h>                    //视频播放
#import "TZAssetModel.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>

@interface TZVideoPlayerController ()<ZFPlayerDelegate>

@property (nonatomic,strong)ZFPlayerView *playerView;
 
@property (weak, nonatomic) IBOutlet UIView *backView;

@property (nonatomic,strong)ZFPlayerControlView *controlView;

@end



@implementation TZVideoPlayerController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
 
    
    if ([self.model.asset isKindOfClass:[ALAsset class]]) {
        ALAsset *alAsset = (ALAsset *)self.model.asset;
        ALAssetRepresentation *defaultRepresentation = [alAsset defaultRepresentation];
        NSString *uti = [defaultRepresentation UTI];
        NSURL *videoURL = [[alAsset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
        
        
         [self loadViedo:videoURL];

        
    }
    else if ([self.model.asset isKindOfClass:[PHAsset class]])
    {
        PHAsset *phAsset = self.model.asset;
        if (phAsset.mediaType == PHAssetMediaTypeVideo) {
            PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
            options.version = PHImageRequestOptionsVersionCurrent;
            options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
            
            PHImageManager *manager = [PHImageManager defaultManager];
            [manager requestAVAssetForVideo:phAsset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
                AVURLAsset *urlAsset = (AVURLAsset *)asset;
                
                NSURL *url = urlAsset.URL;
             
                [self loadViedo:url];
            
            }];
        }
    }
    
}


- (void)loadViedo:(NSURL *)videoUrl{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        
        // view
        _controlView = [[ZFPlayerControlView alloc] init];
        
        //    controlView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        
        
        // model
        ZFPlayerModel *playerModel = [[ZFPlayerModel alloc] init];
        playerModel.fatherView = self.view;
        playerModel.videoURL = videoUrl;
        playerModel.title = @"视频详情";
        
        self.playerView = [[ZFPlayerView alloc] init];
        
        [self.playerView playerControlView:_controlView playerModel:playerModel];
        
        // 打开预览图
        self.playerView.hasPreviewView = YES;
        
        self.playerView.delegate = self;
        
        [self.view addSubview:self.playerView];
        
        
        // delegate
        //        self.playerView.delegate = self;
        // auto play the video
        [self.playerView autoPlayTheVideo];
        

    });
    
  
    
 
}
/** 返回按钮事件 */
- (void)zf_playerBackAction{


    [self dismissViewControllerAnimated:YES completion:nil];


}

@end
